package com.lizhivscaomei.dueros.tobaccoguider.repo;

import cn.hutool.core.collection.CollectionUtil;
import com.lizhivscaomei.dueros.tobaccoguider.entity.TobaccoInfo;
import com.lizhivscaomei.dueros.tobaccoguider.mapper.es.TobaccoInfoRepository;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service("TobaccoInfoRepoEs")
public class TobaccoInfoRepoEs implements TobaccoInfoRepo {

    @Autowired
    TobaccoInfoRepository tobaccoInfoRepository;

    @Override
    public List<TobaccoInfo> list(TobaccoInfo params) {

        List<TobaccoInfo> detailList = new ArrayList<>();
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        if (StringUtils.isNotEmpty(params.getCompany())) {
            query.must(QueryBuilders.matchQuery("company.keyword", params.getCompany()));
        }
        if (StringUtils.isNotEmpty(params.getSizeType())) {
            query.must(QueryBuilders.matchQuery("sizeType.keyword", params.getSizeType()));
        }
        if (StringUtils.isNotEmpty(params.getBrand())) {
            query.must(QueryBuilders.matchQuery("brand.keyword", params.getBrand()));
        }
        if (params.getSmallSalePrice() > 0) {
            double price = params.getSmallSalePrice();
            if (price < 50) {
                query.filter(QueryBuilders.termQuery("smallSalePrice", price));
            } else if (price >= 50 && price <= 100) {
                query.filter(QueryBuilders.rangeQuery("smallSalePrice").gte(price - 5).lte(price + 5));
            } else if (price <= 500) {
                query.filter(QueryBuilders.rangeQuery("smallSalePrice").gte(price - 10).lte(price + 10));
            } else {
                query.filter(QueryBuilders.rangeQuery("smallSalePrice").gte(price - 50).lte(price + 50));
            }
        }
        this.tobaccoInfoRepository.search(new NativeSearchQueryBuilder().withQuery(query).withSort(SortBuilders.fieldSort("typeOrderCode.keyword").order(SortOrder.ASC)).build()).forEach(detail -> detailList.add(detail));

        return detailList;
    }

    @Override
    public Optional<TobaccoInfo> detail(TobaccoInfo query) {
        QueryBuilder queryCmd = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("brand.keyword", query.getBrand()))
                .must(QueryBuilders.matchQuery("spec.keyword", query.getSpec()));
        List<TobaccoInfo> introduceList = new ArrayList<>();
        this.tobaccoInfoRepository.search(queryCmd).forEach(introduce -> introduceList.add(introduce));
        if (CollectionUtil.isNotEmpty(introduceList)) {
            return Optional.ofNullable(introduceList.get(0));
        }
        return Optional.empty();
    }

    @Override
    public TobaccoInfo findByBoxCode(String boxCode) {
        return null;
    }
}
